﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class RoomEndPoints
    {
        private const string RoomsUrl = "api/rooms";
        private HttpClient client;

        public RoomEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<List<RoomViewModel>> GetAllRoomsForCinemaAsync(int cinemaId)
        {
            List<RoomViewModel> rooms = new List<RoomViewModel>();
            HttpResponseMessage response = await client.GetAsync("api/roomsForCinema/" + cinemaId);
            if (response.IsSuccessStatusCode)
            {
                rooms = await response.Content.ReadAsAsync<List<RoomViewModel>>();
            }
            return rooms;
        }

        public async Task<RoomViewModel> GetRoomAsync(int id)
        {
            RoomViewModel room = null;
            HttpResponseMessage response = await client.GetAsync(RoomsUrl + "?id=" + id);
            if (response.IsSuccessStatusCode)
            {
                room = await response.Content.ReadAsAsync<RoomViewModel>();
            }
            return room;
        }

        public async Task CreateRoomAsync(RoomViewModel room)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(RoomsUrl, room);
            response.EnsureSuccessStatusCode();
        }

        public async Task UpdateRoomAsync(RoomViewModel room)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(RoomsUrl, room);
            response.EnsureSuccessStatusCode();
        }

        public async Task DeleteRoomAsync(int id)
        {
            HttpResponseMessage response = await client.DeleteAsync(RoomsUrl + "?id=" + id);
            response.EnsureSuccessStatusCode();
        }
    }
}
